package com.song.controller.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.song.pojo.Category;
import com.song.service.CategoryService;
import com.song.utils.DataGridViewResult;
import com.song.utils.SystemConstant;
import com.song.vo.CategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 宋超
 * HP - the login name of the current user.
 * 2020/12/21 - the current system date.
 * 16:39 - the current system time.
 */
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    /**
     * 查询商品类型列表
     * @param categoryVo
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult list(CategoryVo categoryVo){
        //设置分页信息
        PageHelper.startPage(categoryVo.getPage(),categoryVo.getLimit());
        //查询楼层列表
        List<Category> categoryList = categoryService.findCategoryList(categoryVo);
        //创建分页对象
        PageInfo<Category> pageInfo = new PageInfo<Category>(categoryList);
        //返回分页信息
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }


    /**
     * 添加类型
     * @param category
     * @return
     */
    @RequestMapping("/addCategory")
    public String addFloor(Category category){
        Map<String,Object> map = new HashMap<String, Object>();
        if (categoryService.addCategory(category)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"添加成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"添加失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 修改类型
     * @param category
     * @return
     */
    @RequestMapping("/updateCategory")
    public String updateFloor(Category category){
        Map<String,Object> map = new HashMap<String, Object>();
        if (categoryService.updateCategory(category)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"修改成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"修改失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 查询所有类型
     * @return
     */
    @RequestMapping("/categoryList")
    public String findAll(){
        return JSON.toJSONString(categoryService.findCategoryList(null));
    }

}
